package com.chancingpack.service.impl;

import com.chancingpack.bean.WxSendMessageBean;
import com.chancingpack.dal.dao.WxSendMessageDao;
import com.chancingpack.dal.model.WxSendMessage;
import com.chancingpack.service.WxSendMessageService;
import com.chancingpack.util.CommonPage;
import com.chancingpack.util.CommonResult;
import com.chancingpack.util.ObjectUtil;
import com.chancingpack.util.VelocityUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lenovo on 2017/7/14.
 */
@Service
public class WxSendMessageServiceImpl implements WxSendMessageService {

    @Autowired
    private WxSendMessageDao wxSendMessageDao;

    private static Logger logger = Logger.getLogger(WxSendMessageService.class);
    /**
     * 录入微信收货信息数据
     * @param wxSendMessageBean
     * @return
     */
    public boolean insertSendMessage(WxSendMessageBean wxSendMessageBean) {
        WxSendMessage wxSendMessage = (WxSendMessage)ObjectUtil.transfer(wxSendMessageBean,WxSendMessage.class);
        return wxSendMessageDao.insertSendMessage(wxSendMessage);
    }

    public List<Map<String, Object>> selectCustName() {

        return wxSendMessageDao.selectcustName();
    }
    /**
     * 分页查询微信收发货信息
     *通过客户id
     *
     */
    public CommonResult selectSendpage(CommonPage commonPage, HttpServletRequest request, HttpServletResponse response) {
        CommonResult result = new CommonResult();
        try {

            Map<String,Object> mapsend = new HashMap<String, Object>();
            int curr=commonPage.getCurr();
            int pageSize =commonPage.getPageSize();
           mapsend = commonPage.getParamMap();
            mapsend.put("pagesize",curr*pageSize);
            mapsend.put("num",(curr-1)*pageSize);
            List<Map<String,Object>> SendListMap=wxSendMessageDao.selectSendpage(mapsend);
            if(wxSendMessageDao.selectCount(mapsend) % pageSize>0) {
                commonPage.setPages(wxSendMessageDao.selectCount(mapsend) / pageSize+1);
            }else {
                commonPage.setPages(wxSendMessageDao.selectCount(mapsend) / pageSize);
            }
            for (int i=0;i<SendListMap.size();i++){
                Map<String,Object> map = new HashMap<String, Object>();
                SimpleDateFormat formatter1 = new SimpleDateFormat("yyyy-MM-dd ");
                String ORDER_DATE = formatter1.format(SendListMap.get(i).get("ORDER_DATE"));
                SendListMap.get(i).put("ORDER_DATE",ORDER_DATE);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                String signup_time = formatter.format(SendListMap.get(i).get("SIGNUP_TIME"));
                SendListMap.get(i).put("SIGNUP_TIME",signup_time);
                map.put("detailId",Integer.parseInt(SendListMap.get(i).get("detail_id").toString()));
                if( wxSendMessageDao.selectBydetailId(map).size()==0){
                    SendListMap.get(i).put("count",0);
                }else{
                    if(wxSendMessageDao.selectByReceive(map).size()==0){
                        SendListMap.get(i).put("count",1);
                    }else {
                        SendListMap.get(i).put("count",2);
                    }
                }
                if(curr==1) {
                    SendListMap.get(i).put("viewId", i + 1);
                }else{
                    SendListMap.get(i).put("viewId",i+1+((curr-1)*15));
                }
            }
            commonPage.setPageData(SendListMap);
            Map<String, Object> showMap = new HashMap<String, Object>();
            showMap.put("SendList", commonPage.getPageData());
            commonPage.setHtml(VelocityUtil.getHtmlByMap("logistics/selectListData.vm", showMap,request,response,request.getSession().getServletContext()));
            result.setIsSuccess(true);
        } catch (Exception e) {
            logger.error("查询异常", e);
        }
        return result;
    }
    /**
     * 查询微信发货信息
     *
     *
     */
    public List<Map<String, Object>> selectAll(Map<String, Object> map) {
        return wxSendMessageDao.selectSend(map);
    }
    /**
     * 录入微信收货信息数据
     *
     *
     */
    public CommonResult addtsend(WxSendMessage record) {
        CommonResult result = new CommonResult();
        try {
            if(wxSendMessageDao.insertsend(record)){
                result.setIsSuccess(true);
                result.setResultMsg("添加成功");
            }else {
                result.setIsSuccess(false);
                result.setResultMsg("添加失败");
            }
        }catch (Exception e){

            logger.error("添加发货信息异常", e);
        }
        return result;
    }
    /**
     * 通过明细id查找发货详情
     *
     *
     */
    public List<Map<String, Object>> selectSendBydetailId(Map<String, Object> map) {
        List<Map<String, Object>> sendList = new ArrayList<Map<String, Object>>();
        try {
            sendList = wxSendMessageDao.selectSendBydetailId(map);
        }catch (Exception e){
            logger.error("查询发货信息异常", e);
        }
        return sendList;
    }


}
